package com.gzx.risk.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class AuthManagerConfig {

    private final UserDetailsService userDetailsService;
    private final PasswordEncoder passwordEncoder;

    // 注入自定义的 UserDetailsService 和 PasswordEncoder
    public AuthManagerConfig(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        this.userDetailsService = userDetailsService;
        this.passwordEncoder = passwordEncoder;
    }

    @Bean
    public AuthenticationManager customAuthenticationManager() {
        // 1. 创建用户名密码认证提供者
        DaoAuthenticationProvider daoProvider = new DaoAuthenticationProvider();
        daoProvider.setUserDetailsService(userDetailsService); // 关联用户服务
        daoProvider.setPasswordEncoder(passwordEncoder); // 关联密码加密器

        // 2. 可添加其他认证提供者（如验证码认证、令牌认证等）
        // CustomAuthenticationProvider customProvider = new CustomAuthenticationProvider();

        // 3. 构建 AuthenticationManager，传入所有认证提供者
        return new ProviderManager(daoProvider /*, customProvider */);
    }
}
